<?php

use Doraemon\service\Comment;
use Doraemon\model\CommentLink;
use Biz\service\wiki\ObjRelateWiki;
use Doraemon\model\Comment as DbComment;
use Doraemon\tools\Log;

class DelCommentAction extends ActionPlugin
{
    const IS_TRACING = true;

    /**
     * @throws Exception
     */
    public function main()
    {

        $table = $this->messageBody['table'] ?? '';
        $data = $this->messageBody['data']?? [];


        \Doraemon\tools\Log::info('删除评论记录', $this->messageBody);
        $aTable = [
            'hhz_comments_0',
            'hhz_comments_1',
            'hhz_comments_2',
            'hhz_comments_3',
        ];

        if (!in_array($table, $aTable)) {
            throw new \Exception('数据错误，不是内容表数据');
        }
        $res = $this->delCommentsUnContact($data);
        $log['params'] = $this->messageBody;
        $log['res'] = $res;

        Log::info("删除评论", $log);
        $this->setSuccess();
    }


    /**
     * 删除评论 解除相关关联操作
     *
     * @param $data
     * @return bool
     * @throws Exception
     * @time 2022/11/14 14:27
     * @desc
     */
    public function delCommentsUnContact($data): bool
    {
        if (!in_array($data['status'],
            [DbComment::STATUS_DEL, DbComment::STATUS_ADM_DELETE, DbComment::STATUS_OWNER_DELETE], false)) {
            return false;
        }

        // 删除链接表信息
        $updateLinkData = [
            "status" => CommentLink::STATUS_DEL
        ];
         CommentLink::update($updateLinkData,
            ["comments_id" => $data['id'], "obj_id" => $data['obj_id']]);
        // 评论删除 删除wiki信息
        $wikiObjType = Comment::getCommentRelateWikiType($data['obj_id']);
        if ($wikiObjType) {
            ObjRelateWiki::delCommentRelate($data["uid"], $data['obj_id'], $wikiObjType, $data['id']);
        }
        return true;
    }


}